package com.hx.study.service.impl;

import java.util.List;

import com.hx.common.utils.DateUtils;
import com.hx.common.utils.StringUtils;
import com.hx.study.domain.Question;
import com.hx.study.domain.QuestionAnswer;
import com.hx.study.mapper.QuestionMapper;
import com.hx.study.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import org.springframework.transaction.annotation.Transactional;

/**
 * 问题题目Service业务层处理
 *
 * @author user
 * @date 2022-06-06
 */
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询问题题目
     *
     * @param id 问题题目主键
     * @return 问题题目
     */
    @Override
    public Question selectQuestionById(Integer id) {
        return questionMapper.selectQuestionById(id);
    }

    /**
     * 查询问题题目列表
     *
     * @param question 问题题目
     * @return 问题题目
     */
    @Override
    public List<Question> selectQuestionList(Question question) {
        return questionMapper.selectQuestionList(question);
    }

    /**
     * 新增问题题目
     *
     * @param question 问题题目
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQuestion(Question question) {
        question.setCreateTime(DateUtils.getNowDate());
        int rows = questionMapper.insertQuestion(question);
        insertQuestionAnswer(question);
        return rows;
    }

    /**
     * 修改问题题目
     *
     * @param question 问题题目
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQuestion(Question question) {
        question.setUpdateTime(DateUtils.getNowDate());
        questionMapper.deleteQuestionAnswerByQuestionId(question.getId());
        insertQuestionAnswer(question);
        return questionMapper.updateQuestion(question);
    }

    /**
     * 批量删除问题题目
     *
     * @param ids 需要删除的问题题目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionByIds(Integer[] ids) {
        questionMapper.deleteQuestionAnswerByQuestionIds(ids);
        return questionMapper.deleteQuestionByIds(ids);
    }

    /**
     * 删除问题题目信息
     *
     * @param id 问题题目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionById(Integer id) {
        questionMapper.deleteQuestionAnswerByQuestionId(id);
        return questionMapper.deleteQuestionById(id);
    }

    /**
     * 新增候选答案信息
     *
     * @param question 问题题目对象
     */
    public void insertQuestionAnswer(Question question) {
        List<QuestionAnswer> questionAnswerList = question.getQuestionAnswerList();
        Integer id = question.getId();
        if (StringUtils.isNotNull(questionAnswerList)) {
            List<QuestionAnswer> list = new ArrayList<QuestionAnswer>();
            for (QuestionAnswer questionAnswer : questionAnswerList) {
                questionAnswer.setQuestionId(id);
                list.add(questionAnswer);
            }
            if (list.size() > 0) {
                questionMapper.batchQuestionAnswer(list);
            }
        }
    }

    @Override
    public List<Question> selectRandByRepositoryIdAndType(String repositoryId, String type, int count) {
        return questionMapper.selectRandByRepositoryIdAndType(repositoryId, type, count);
    }
}
